package com.gxa.service.shezhiservice.Impl;

import com.gxa.common.R;
import com.gxa.mapper.shezhimapper.ZhangHuMapper;
import com.gxa.pojo.ZhangHu;
import com.gxa.pojo.ZhangHuSheZhi;
import com.gxa.service.shezhiservice.ZhangHuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author weishen
 */
@Service
public class ZhangHuServiceImpl implements ZhangHuService {

    @Autowired
    ZhangHuMapper zhangHuMapper;

    @Override
    public R selectAllZhangHuForPage(int page, int size) {
        R r= new R();
        int index = (page-1)*size;
        List<ZhangHu> zhangHus = zhangHuMapper.selectAllZhangHuForPage(index, size);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setCount(selectCountZhangHu());
        r.setData(zhangHus);
        return r;
    }

    @Override
    public R updateZhangHuById(ZhangHuSheZhi zhangHu) {
        R r = new R();
        int res = zhangHuMapper.updateZhangHuById(zhangHu);
        r.setCode(500);
        r.setMsg("更新失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("更新成功");
        }
        return r;
    }

    @Override
    public R insertZhangHu(ZhangHuSheZhi zhangHu) {
        R r = new R();
        int res = zhangHuMapper.insertZhangHu(zhangHu);
        r.setCode(500);
        r.setMsg("新增失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("新增成功");
        }
        return r;
    }

    @Override
    public R deleteZhangHuById(ArrayList<Integer> id) {
        R r = new R();
        int res=0;
        int flag=1;
        for(int i=0;i<id.size();i++)
        {
            res=zhangHuMapper.deleteZhangHuById(id.get(i));
            if(res==0)
            {
                flag=0;
                break;
            }
        }
        r.setCode(200);
        r.setMsg("删除成功");
        if(flag==0)
        {
            r.setCode(500);
            r.setMsg("删除失败");
        }
        return r;
    }

    @Override
    public int selectCountZhangHu() {
        return zhangHuMapper.selectCountZhangHu();
    }
}
